#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
APP_INSTALL_NAME="spark-tts"
APP_TARGET_DIR_NAME="spark-tts"

if [ -f /etc/os-release ]; then
   echo "Linux system."
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
   echo "macOS system."
   if [ -f ~/.bash_profile ]; then
      source ~/.bash_profile
    fi
elif current_os_is_windows; then
   echo "Windows system."
else
   echo "Unknown OS"
fi

echo "lmdprogress:3"
source "$LMD_BASE_INSTALL_SCRIPT_DIR/global/install_conda.sh"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo "$CURRENT_APP_PATH"
mkdir -p "$CURRENT_APP_PATH"

echo "lmdprogress:8"
if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: $CURRENT_APP_PATH"
else
    echo "Error, App Dir dose not exist, please retry. $CURRENT_APP_PATH"
    exit 1
fi

echo "lmdprogress:12"

export GIT_HOST=${GITHUB_PROXY}"https://github.com"
export GIT_REPO_URL=${GIT_HOST}/sparkaudio/spark-tts

export GIT_REPO_BRANCH="main"
export LOCAL_REPO_PATH="$CURRENT_APP_PATH"
sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"


echo "lmdprogress:40"

source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
conda --version

echo "lmdprogress:43"

# SPARK_TTS_ENV_NAME=sparktts
cd "$CURRENT_APP_PATH"
CONDA_PREFIX="./venv"

if [ -d "$CONDA_PREFIX" ]; then
    echo "found ./venv. remove it. "
    rm -rf "$CONDA_PREFIX"
fi

export PYTHON_VER_ENV=3.12
conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV -y

echo "lmdprogress:45"
if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    conda update conda -n base -y
fi

echo "lmdprogress:50"
install_torch() {
    if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
        # conda_run_pip_install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
        # conda_run_pip_install torch==2.8.0 torchvision torchaudio
        source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_mac.sh"
    elif current_os_is_windows; then
        # source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_win.sh"
        source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_multi_method.sh"
    fi
}
install_torch

echo "lmdprogress:60"

# create temp file
rm -rf requirements-temp.txt
cp requirements.txt requirements-temp.txt
grep -v "torch" requirements.txt > requirements-temp.txt
conda_run_pip_install --pre -r requirements-temp.txt

echo "lmdprogress:70"
conda_run_pip_install gradio==5.39.0 modelscope==1.29.1 huggingface-hub
echo "lmdprogress:75"

echo "install model: SparkAudio/Spark-TTS-0.5B"
if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi

if [ -n "$HF_ENDPOINT" ]; then
    conda_run_cmd modelscope download --model SparkAudio/Spark-TTS-0.5B --local_dir ./pretrained_models/Spark-TTS-0.5B
else
    conda_run_cmd python "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/download_model.py" 
fi


echo "lmdprogress:100"
